import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const routes = [
  {
    path: '/',
    name: 'Home',
    meta: {
      isTable: true
    },
    component: () => import('../views/Home.vue')
  },
  {
    path: '/about',
    name: 'About',
    meta: {
      isTable: true
    },
    component: () => import('../views/About.vue')
  },
  {
    path: '/product',
    name: 'Product',
    meta: {
      isTable: true
    },
    component: () => import('../views/Product.vue')
  },
  {
    path: '/friend',
    name: 'Friend',
    meta: {
      isTable: true
    },
    component: () => import('../views/Friend.vue')
  },
  {
    path: '/my',
    name: 'My',
    meta: { auth: true, isTable: true },
    component: () => import('../views/My.vue')
  },
  {
    path: '/login',
    name: 'Login',
    meta: {
      isTable: true
    },
    component: () => import('../views/Login.vue')
  },
  {
    path: '/register',
    name: 'Register',
    meta: {
      isTable: true
    },
    component: () => import('../views/Register.vue')
  },
  {
    path: '/reset',
    name: 'resetPassword',
    meta: {
      isTable: true
    },
    component: () => import('../views/ResetPassword.vue')
  },
  {
    path: '/order',
    name: 'Order',
    meta: { auth: true },
    component: () => import('../views/Order.vue')
  },
  {
    path: '/after',
    name: 'After',
    meta: { auth: true },
    component: () => import('../views/After.vue')
  },
  {
    path: '/add-order',
    name: 'AddOrder',
    meta: { auth: true },
    component: () => import('../views/AddOrder.vue')
  },
  {
    path: '/launch-after',
    name: 'LaunchAfter',
    meta: { auth: true },
    component: () => import('../views/LaunchAfter.vue')
  },
  {
    path: '/activity',
    name: 'Activity',
    meta: { auth: true },
    component: () => import('../views/Activity.vue')
  },
  {
    path: '/address',
    name: 'Address',
    meta: { auth: true },
    component: () => import('../views/Address.vue')
  },
  {
    path: '/add-address',
    name: 'AddAddress',
    meta: { auth: true },
    component: () => import('../views/AddAddress.vue')
  }
]

const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
})


router.beforeEach((to, from, next) => {
  if (to.meta.auth) {
    if (!localStorage.getItem('token')) {
      next('/login')
    } else {
      next()
    }
  } else {
    next()
  }

})

export default router
